<template>
  <j-modal
    :title="title"
    :width="600"
    :visible="visible"
    switchFullscreen
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    @ok="handleOk"
  >
    <a-form-model layout="horizontal" ref="realForm" :model="model" :rules="validatorRules" v-bind="formItemLayout">
      <z-form-item :datas="fieldArr" :form="model" :rules="validatorRules" :gutter="0">
        <template slot="slots-target">
          <a-select mode="tags" v-model="model.targets" :disabled="fieldArr[1].disabled" :options="targetOptions" placeholder="请选择目标人员" @change="handleChange" />
        </template>
        <template slot="slots-riskLevel">
          <a-radio-group v-model="model.riskLevel" :disabled="fieldArr[2].disabled">
            <a-radio v-for="(item, key) in riskLevelDatas" :key="key" :value="item.value">{{ item.text }}</a-radio>
          </a-radio-group>
        </template>
      </z-form-item>
    </a-form-model>
  </j-modal>
</template>

<script>
import { taskDisposal, getTaskDetails, getTargetObject } from '@/api/sjsk/alarmService'
export default {
  props: ['riskLevelDatas'],
  data() {
    return {
      title: '处置',
      visible: false,
      disableSubmit: false,
      disabled: false,
      model: {},
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      targetOptions: [],
      fieldArr: [
        {label: '任务名称', prop: 'taskName', tagType: 'input', disabled: true},
        {label: '目标对象', prop: 'target', disabled: false},
        {label: '风险级别', prop: 'riskLevel', disabled: false},
        {label: '其他说明', prop: 'otherInstructions', tagType: 'textarea', disabled: false},
      ],
      validatorRules: {
        disposalInstructions:[ { required: true, message: '请输入处置说明' }]
      }
    }
  },
  created() {},
  methods: {
    show(record, type) {
      this.visible = true
      this.$set( this.model, 'taskName', record.taskName )
      this.$set( this.model, 'taskId', record.id )
      if(type === 'add') {
        this.title = '处置'
        this.disableSubmit = false
        this.fieldArr.forEach(item=> {
          if(item.prop=='target') item.disabled = false
          if(item.prop=='riskLevel') item.disabled = false
          if(item.prop=='otherInstructions') item.disabled = false
        })
      } else if(type === 'details') {
        this.title = '处置结果'
        this.disableSubmit = true
        this.fieldArr.forEach(item=> {
          if(item.prop=='target') item.disabled = true
          if(item.prop=='riskLevel') item.disabled = true
          if(item.prop=='otherInstructions') item.disabled = true
        })
        this.getTaskDetails(record.id)
      }
      this.getTargetObject(record.id)
    },
    getTargetObject(taskCode) {
      getTargetObject(taskCode).then((res) => {
        if (res.success) {
          const { result } = res
          this.targetOptions = result.map(item=> {
            return {
              key: item.id.toString(),
              title: item.name+'（'+item.phone+'）',
              id: item.id,
              name: item.name,
              phone: item.phone
            }
          })
        } else {
          this.$message.warn(res.message)
        }
      }).catch(e=>{
        that.$message.warn("操作失败！")
      })
    },
    getTaskDetails(taskCode) {
      getTaskDetails(taskCode).then((res) => {
        if (res.success) {
          const { result } = res
          this.$set( this.model, 'otherInstructions', result.otherInstructions )
          this.$set( this.model, 'riskLevel', result.riskLevel.toString() )
          this.$set( this.model, 'targets', result.target.map(v=>v.id.toString()))
        } else {
          this.$message.warn(res.message)
        }
      }).catch(e=>{
        that.$message.warn("操作失败！")
      })
    },
    handleOk() {
      const parameter = Object.assign({}, this.model)
      this.$refs.realForm.validate((valid)=> {
        if (valid) {
          taskDisposal(parameter)
            .then(res => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
                this.handleCancel()
              } else {
                this.$message.warn(res.message)
              }
            })
            .catch(e => {
              this.$message.warn('请求失败！')
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleCancel() {
      this.visible = false
      this.model = {}
    },
    handleChange(value, datas) {
      let maps = datas.map(v=> v.data.props)
      this.$set( this.model, 'target', maps)
    },
  }
}
</script>
<style lang="less" scoped>
</style>
